import api from '@/services/api';
import { type InputLogin, type InputImportUser, Role} from '@/services/users/types';

// 登录
async function login(data: InputLogin) {
  return await api.post('/api/login/', data)
}

// 获取用户信息
async function getUserInfo(role: Role, id: number) {
  let url = '';
  if (role === Role.Student) {
    url = `/api/student/${id}/info/`;
  } else if (role === Role.Teacher) {
    url = `/api/teacher/${id}/info/`;
  }
  return await api.get(url);
}

async function importUserInfo(input: InputImportUser) {
  if (!input.csv_file) {
    throw new Error('文件为空');
  }
  console.log({csv_file: input.csv_file});
  let url = '';
  if (input.registerRole == Role.Teacher) {
    url = '/api/admins/import_teacher/';
  }else if (input.registerRole == Role.Student) {
    url = '/api/admins/import/';
  }
  return await api.post(url, {csv_file: input.csv_file}, {headers: {
    'Content-Type': 'multipart/form-data'
  }});
}

async function modifyUserInfo(role: Role, id:number, mail: string, phone: string) {
  let url = '';
  if (role === Role.Student) {
    url = `/api/student/${id}/adjust-info/`;
  } else if (role === Role.Teacher) {
    url = `/api/teacher/${id}/adjust-info/`;
  }
  return await api.post(url, {
    email: mail,
    phoneNumber: phone,
  });

}

async function getNotifycation(role: Role, id: number) {
  let url = '';
  if (role === Role.Student) {
    url = `/api/student/${id}/system-notices/`;
  } else if (role === Role.Teacher) {
    url = `/api/teacher/${id}/system-notices/`;
  }
  return await api.get(url);
}

async function getAllUser() {
  return await api.get('/api/admins/get_all_users/');
}

async function sendNotfication(list :{id:string, role:string}[], content: string) {
  console.log(list);
  return await api.post('/api/admins/publish_system_notice/', {
    mentionList: list,
    content: content,
  });
}

async function resetPassword( InputResetPassword: {
  account: string,
  phoneNumber: string,
  new_password: string,
  confirm_password: string
}) {
  return await api.post('/api/resetPassword/', InputResetPassword)
}
export default {
  login,
  getUserInfo,
  importUserInfo,
  modifyUserInfo,
  getAllUser,
  getNotifycation,
  sendNotfication,
  resetPassword,
};
